package com.agou.ssh.core.base;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.agou.app.AppContext;
import com.agou.core.ss.SSException;
import com.agou.core.ss.SSLogicException;
import com.agou.core.ss.SSTrace;
import com.agou.ssh.web.WebUtil;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport implements IBaseAction {
	private static final long serialVersionUID = -6112585696400679814L;

	private static SSTrace ssTrace = SSTrace.getInstance(BaseDao.class);
	private SessionFactory _sessionFactory;
	private Session _jdbcSession;
	private Transaction _jdbcTransaction;
	private boolean _isTransaction = false;// 是否有事务
	private boolean _autoTransaction = true;// 自动事务控制

	// Spring管理
	public SessionFactory getSessionFactory() {
		return _sessionFactory;
	}

	// Spring管理
	public void setSessionFactory(SessionFactory sessionFactory) {
		ssTrace.debug("setSessionFactory", "创建session对象:#0",sessionFactory);
		this._sessionFactory = sessionFactory;
	}

	public Transaction getJdbcTransaction() {
		return _jdbcTransaction;
	}

	public void jdbcBeging(boolean isTransaction, boolean autoTransaction) {
		ssTrace.debug("jdbcBeging", "-----------------开始执行事务-----------------");
		_isTransaction = isTransaction;// 是否事务
		_autoTransaction = autoTransaction;// 是事自动控制事务
		if (_jdbcSession == null) {
			_jdbcSession = _sessionFactory.openSession();//
		} else {
			ssTrace.warn("getJdbcSession", "jdbc session 已存在.打开状态是：#0",_jdbcSession.isOpen());
		}
		if (_isTransaction == true) {
			_jdbcTransaction = _jdbcSession.beginTransaction();// 设置事务
		}
	}

	public Session getJdbcSession() {
		return _jdbcSession;
	}

	public void transactionBegin() throws SSException {
		if (_jdbcSession == null) {
			ssTrace.error("transactionBegin", "请先调用getJdbcSession方法，才能进行事务");
			throw new SSLogicException(1, "请先调用getJdbcSession方法，才能进行事务");
		}
		if (_jdbcTransaction == null) {
			ssTrace.warn("transactionBegin", "建议在getJdbcSession中直接设置事务");
			_jdbcTransaction = _jdbcSession.beginTransaction();// 设置事务
		}else{
			_jdbcTransaction.begin();
		}
		_isTransaction = true;// 是否事务｛重置为true｝
	}

	public void transactionRollback() throws SSException {
		ssTrace.debug("jdbcBeging", "-----------------事务回滚-----------------");
		_isTransaction = false;
		_jdbcTransaction.rollback();
	}

	public void transactionCommit() throws SSException {
		ssTrace.debug("jdbcBeging", "-----------------事务提交-----------------");
		_isTransaction = false;
		_jdbcTransaction.commit();
	}

	// 销毁方法
	public void destroy() throws SSException, IOException, SQLException {
		if (_isTransaction == true) {
			if (_autoTransaction) {
				this.transactionCommit();
			}
		}
		_jdbcTransaction = null;
		if (_jdbcSession != null) {
			_jdbcSession.close();
			_jdbcSession = null;
		}
	}

	public void destroyInError() throws SSException, IOException, SQLException {
		if (_isTransaction == true) {
			if (_autoTransaction) {
				this.transactionRollback();
			}
		}
	}

	//
	public HttpServletRequest getHttpRequest() throws SSException,
			SQLException, IOException {
		return ServletActionContext.getRequest();
	}
	
	public AppContext getAppContext(){
		return WebUtil.getAppContext();
	}
	
	/***处理一些系统怪异事情**/
	private String _;//使用jquery必须需要 _ 这个参数

	public String get_() {
		return _;
	}

	public void set_(String _) {
		this._ = _;
	}
	
}
